package cn.labbook.bluetooth.service.impl;

import cn.labbook.bluetooth.entity.Admin;
import cn.labbook.bluetooth.entity.Ammeter;
import cn.labbook.bluetooth.entity.StaticsEntity.ManufactorStatistics;
import cn.labbook.bluetooth.entity.User;
import cn.labbook.bluetooth.mapper.AmmeterMapper;
import cn.labbook.bluetooth.pojo.dto.AmmeterDTO;
import cn.labbook.bluetooth.pojo.dto.QrCodeDTO;
import cn.labbook.bluetooth.service.IAdminService;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.IConstantService;
import cn.labbook.bluetooth.service.IUserService;
import cn.labbook.bluetooth.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lonzy
 * @since 2019-03-26
 */
@Service
public class AmmeterServiceImpl extends ServiceImpl<AmmeterMapper, Ammeter> implements IAmmeterService {

    @Autowired
    private AmmeterMapper ammeterMapper;
    @Autowired
    private IConstantService constantService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IAdminService adminService;


    @Override
    public ManufactorStatistics getStatisticsByManufactor(String manufacturersId) {
        return ammeterMapper.getStatisticsByManufactor(manufacturersId);
    }
    @Override
    public ManufactorStatistics getStatisticsByDealer(String dealerId) {
        return ammeterMapper.getStatisticsByDealer(dealerId);
    }

    @Override
    public IPage<Ammeter> groupByPage(String adminId,String status ,IPage<Ammeter> page) {
        QueryWrapper<Ammeter> lambda=new QueryWrapper<>();
        lambda.eq("admin_id", adminId);
        lambda.eq("status", status);
        lambda.groupBy("manager_id");
        IPage<Ammeter> ammeterIPage = ammeterMapper.groupByPage(page,lambda);
        return ammeterIPage;
    }

    @Override
    public AmmeterDTO getAmmeter(String ammeterId) {
        AmmeterDTO dto = new AmmeterDTO();
        Ammeter ammeter = getById(ammeterId);
        if (Objects.isNull(ammeter)) {
            return dto;
        }

        dto.setLocation(ammeter.getLocation());
        dto.setBalance(ammeter.getBalance());
        dto.setRechargeAmount(ammeter.getRechargeAmount());
        dto.setProtectPrice(getProtectPrice(ammeter.getProtectPrice()));
        dto.setIsSkimp(ammeter.getIsSkimp());
        return dto;
    }

    @Override
    public double getProtectPrice(Double protectPrice) {
        if (Objects.isNull(protectPrice)) {
            return constantService.getProtectPrice();
        }
        return protectPrice;
    }

    @Override
    public QrCodeDTO getQrCode(String ammeterId) {
        QrCodeDTO dto = new QrCodeDTO();
        Ammeter ammeter = getById(ammeterId);
        if (Objects.isNull(ammeter)) {
            return dto;
        }
        User user = userService.getByAdminId(ammeter.getManagerId());

        dto.setQrCodeWeixin(user.getQrCodeWeixin());
        dto.setQrCodeAli(user.getQrCodeAli());
        return dto;
    }

    @Override
    public boolean isLandlord(String managerId) {
        if (StringUtils.isBlank(managerId)) {
            return false;
        }
        QueryWrapper<Ammeter> lambda = new QueryWrapper<>();
        lambda.eq("manager_id", managerId);
        return count(lambda) != 0;
    }

    // 获取费率
    public double getNowPrice(String ammeterId) {
        LambdaQueryWrapper<Ammeter> lambda1 = new QueryWrapper<Ammeter>().lambda();
        lambda1.eq(Ammeter::getAmmeterId, ammeterId);
        Ammeter one = getOne(lambda1);

        LambdaQueryWrapper<Admin> lambda2 = new QueryWrapper<Admin>().lambda();
        lambda2.eq(Admin::getAdminId, one.getAdminId());
        Admin one1 = adminService.getOne(lambda2);
        return one1.getPayRate();
    }

}
